﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Data;
using DiemChapNhanThe.Entities;
using System.Data;

public partial class Admin_DanhMuc_DMQuangCao : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiQuangCao(cboLoaiQuangCao);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        using (DataSet dsBookCategories = DataRepository.Provider.ExecuteDataSet("QuangCao_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize))
        {
            gridCatalog.DataSource = dsBookCategories.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsBookCategories.Tables[1].Rows[0][0]);
            }
            catch
            {
            }
        }
        Pager1.ItemCount = TotalRows;
        Pager1.Visible = TotalRows > Pager1.PageSize;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMQuangCao.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (QuangCao entQuangCao = new QuangCao() { Id = Convert.ToInt32(H_CatalogId.Value), TenQuangCao = txtTen.Text, IdLoaiQuangCao = Convert.ToInt32(cboLoaiQuangCao.SelectedValue), ChuThich = txtChuThich.Text, DuongDan = txtDuongDan.Text, TrangThai = chkTrangThai.Checked, NgayCapNhat = Common.GetServerDateTime(), AnhDaiDien = H_AnhDaiDien.Value.Length == 0 ? "no_image.jpg" : Common.StandardString(H_AnhDaiDien.Value) })
            {
                switch (entQuangCao.Id)
                {
                    case 0:
                        entQuangCao.Id = 0;
                        updateStatus = DataRepository.QuangCaoProvider.Insert(entQuangCao);
                        break;
                    default:
                        updateStatus = DataRepository.QuangCaoProvider.Update(entQuangCao);
                        break;
                }
            }

            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG,
                                                    Constants.QUANGCAO_CAPNHAT_THANHCONG, true);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);

                H_CatalogId.Value = "";
                H_AnhDaiDien.Value = "no_image.jpg";
                txtTen.Text = "";
                txtChuThich.Text = "";
                txtDuongDan.Text = "";
                BindCatalog.BindLoaiQuangCao(cboLoaiQuangCao);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI,
                                                    Constants.QUANGCAO_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMQuangCao.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTen.Text = "";
        txtChuThich.Text = "";
        txtDuongDan.Text = "";
        BindCatalog.BindLoaiQuangCao(cboLoaiQuangCao);
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                QuangCao entQuangCao = DataRepository.QuangCaoProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entQuangCao != null)
                {
                    txtTen.Text = entQuangCao.TenQuangCao;
                    txtChuThich.Text = entQuangCao.ChuThich;
                    txtDuongDan.Text = entQuangCao.DuongDan;
                    cboLoaiQuangCao.SelectedValue = entQuangCao.IdLoaiQuangCao.ToString();
                    chkTrangThai.Checked = entQuangCao.TrangThai;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMQuangCao.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdQuangCao = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.QuangCaoProvider.Delete(IdQuangCao);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMQuangCao.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_XOA_THATBAI, true);
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtTen.Text = "";
        txtChuThich.Text = "";
        txtDuongDan.Text = "";
        BindCatalog.BindLoaiQuangCao(cboLoaiQuangCao);
        H_CatalogId.Value = "0";
    }

    public string GetLoaiQuangCao(object Char)
    {
        int idQuangCao = Convert.ToInt32(Char.ToString());
        using (LoaiQuangCao entQuangCao = DataRepository.LoaiQuangCaoProvider.GetById(idQuangCao))
        {
            return entQuangCao.TenLoaiQuangCao;
        }
    }
}